<?php
class Default_Model_DbTable_Photo extends Zend_Db_Table_Abstract {
	protected $_name = 'photo';
	// add new photo
	public function AddPhoto($PLibID, $PName, $PImg, $Stt) {
		$PhotoData = array (
				"PHOTO_LIB_ID" => $PLibID,
				"PHOTO_NAME" => $PName,
				"PHOTO_IMG" => $PImg,
				"STATUS" => $Stt 
		); // print_r($PhotoData);die("debug");
		$this->insert ( $PhotoData );
	}
	// update photo
	public function UpdatePhoto($Id, $PLibID, $PName, $PImg, $Stt) {
		$PhotoData = array (
				"PHOTO_LIB_ID" => $PLibID,
				"PHOTO_NAME" => $PName,
				"PHOTO_IMG" => $PImg,
				"STATUS" => $Stt 
		);
		$this->update ( $PhotoData, "ID = $Id" );
	}
	// remove Photo
	public function DelPhoto($id) {
		return $this->delete ( "ID = $id" );
	}
	// Get photo by name
	public function GetByName($Name) {
		return $Photo = $this->fetchRow ( "PHOTO_NAME = $Name" );
	}
	// get all photo
	public function GetAll() {
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		$select->from ( "photo AS p" )->joinLeft ( "lib_photo AS lp", "p.PHOTO_LIB_ID = lp.ID", array (
				"lp.NAME" 
		) )->where("p.STATUS = 1")->group ( "p.ID" );
		return $this->fetchAll ( $select );
	}
	// get by id
	public function GetByID($id) {
		return $this->fetchRow ( "ID = $id" );
	}
	public function updateStatus($id, $status) {
		$PhotoData = array (
				"STATUS" => $status
		);
		$this->update ( $PhotoData, "ID = $id" );
	}
	// get photo by id
	public function GetByLibId($Id) {
		return $Photo = $this->fetchAll ( "PHOTO_LIB_ID = $Id" )->toArray();
	}
	// get photo by library
	public function GetByLib() {
		$select = $this->select ()->from ( $this->_name, array (
				"PHOTO_LIB_ID",
				"PHOTO_NAME",
				"PHOTO_IMG" 
		) )->where ( "PHOTO_LIB_ID > 0" );
		return $this->fetchAll ( $select )->toArray ();
	}
	// change status
	public function Active($id) {
		$p = $this->GetByID ( $id );
		if ($p ['STATUS'] == 1) {
			$this->update ( array (
					"STATUS" => "0" 
			), "ID = $id" );
		} else {
			$this->update ( array (
					"STATUS" => "1" 
			), "ID = $id" );
		}
	}
}

